<?php

/**
 +------------------------------------------------------------------------------
 文章
 +------------------------------------------------------------------------------
 */

class ArticleAction extends CommonAction{
    function insert() {
        $name=$this->getActionName();
        $model = D ($name);
        if (false === $data = $model->create ()) {
                $this->error ( $model->getError () );
        }

        if($upload_list = $this->uploadImages()) {
            $data['img'] = $upload_list[0]['recpath'].$upload_list[0]['savename'];
            $imginfo = getimagesize($upload_list[0]['savepath'].$upload_list[0]['savename']);
            $data['width'] = $imginfo[0];
            $data['height'] = $imginfo[1];
        }

        $model->add($data);
        $this->redirect('Article/index');
    }
    function edit() {
        vendor("common");
        global $_FANWE;
        $name = $this->getActionName();
        $model = D($name);

        $id = $_REQUEST [$model->getPk ()];
        $vo = $model->getById($id);
        if($vo['img'])
            $vo['img']= $_FANWE['site_url'].$vo['img'];
            
		$cate_list = D("GoodsCategory")->where('status = 1')->field('cate_id,parent_id,cate_name')->order('sort ASC,cate_id ASC')->select();
		$cate_list = D("GoodsCategory")->toFormatTree($cate_list,array('cate_name'),'cate_id');

        $this->assign("cate_list",$cate_list);
        $this->assign ( 'vo', $vo );
        $this->display ();
    }
    function update() {
        $name=$this->getActionName();
        $model = D ($name);
        if (false === $data = $model->create ()) {
                $this->error ( $model->getError () );
        }

        if($upload_list = $this->uploadImages()) {
            $data['img'] = $upload_list[0]['recpath'].$upload_list[0]['savename'];
            $imginfo = getimagesize($upload_list[0]['savepath'].$upload_list[0]['savename']);
            $data['width'] = $imginfo[0];
            $data['height'] = $imginfo[1];
        }
      
//        $img_size = getimagesize($upload_list[0]['savepath'].$upload_list[0]['savename']);
//        if($img_size[0]>320 || $img_size[1]>230){
//            unlink($upload_list[0]['savepath'].$upload_list[0]['savename']);
//            $this->error ('图片尺寸不符合要求！');
//        }
        $list=$model->save($data);
        $this->redirect('Article/index');
    }
    
    function add() {
		$cate_list = D("GoodsCategory")->where('status = 1')->field('cate_id,parent_id,cate_name')->order('sort ASC,cate_id ASC')->select();
		$cate_list = D("GoodsCategory")->toFormatTree($cate_list,array('cate_name'),'cate_id');
        $this->assign("cate_list",$cate_list);
		$this->display ();
	}
	
	public function index() {
		$map = $this->_search();
		if (method_exists($this, '_filter')) {
			$this->_filter ($map);
		}
		$model = D('Article');
		$this->_list ( $model, $map );
		$list = $this->list;
		foreach($list as $k => $v) {
		    $tmp = D('GoodsCategory')->query('SELECT cate_id,cate_name FROM '.C("DB_PREFIX").'goods_category WHERE cate_id='.$v['cate_id']);
		    $list[$k]['cate_typename'] = $tmp[0]['cate_name'];
		}
		$this->assign('list', $list);
		$this->display ();
		return;
	}
}
?>
